using Microsoft.AspNetCore.Mvc;
using zhwy.saas.api.Entity.Sys;
using zhwy.saas.api.Extensions.Enum;
using zhwy.saas.api.Extensions.Utils;
using zhwy.saas.api.Models;
using zhwy.saas.api.Models.Sys.Tenants;
using zhwy.saas.api.Models.Sys.Users;
using zhwy.saas.api.Services.Sinterface;
using zhwy.saas.api.Services.Sinterface.Sys;

namespace zhwy.saas.api.Controllers.V1.Sys
{
    /// <summary>
    /// 系统租户管理
    /// </summary>
    [ApiVersion("1.0")]
    public class SysTenantController : BaseController<TenantsEntity>
    {
        private readonly ISysTenantService _sysTenantService;
        private readonly ICurrentUserService _currentUserService;
        private readonly ISysUserService _sysUserService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysTenantService">租户服务</param>
        public SysTenantController(ISysTenantService sysTenantService,
            ISysUserService sysUserService,
            ICurrentUserService currentUserService)
            : base(sysTenantService)
        {
            _sysTenantService = sysTenantService;
            _sysUserService = sysUserService;
            _currentUserService = currentUserService;
        }

        /// <summary>
        /// 添加新实体
        /// </summary>
        /// <param name="req">实体信息</param>
        /// <returns>添加结果</returns>
        /// <response code="200">添加成功</response>
        /// <response code="400">添加失败</response>
        [HttpPost("add")]
        public async Task<ApiResponseModel<bool>> Add([FromBody] AddTenantsReq req)
        {
            var userid = _currentUserService.GetUserId();
            SnowflakeIdGenerator snowflakeId = new SnowflakeIdGenerator(1, 1);
            var res = false;
            var entity = new TenantsEntity()
            {
                Businesslicense_img = req.Businesslicense_img.Trim(),
                Createtime = DateTime.Now,
                Createuserid = userid,
                Legalperson = req.Legalperson.Trim(),
                Name = req.Name.Trim(),
                Status = (int)EnumHelper.UsersStatus.use,
                Telephone = req.Telephone.Trim(),
                Tenant_code = snowflakeId.NextId().ToString(),
                Updatetime = DateTime.Now,
                Updateuserid = userid,
                Usci = req.Usci.Trim(),
            };
            var result = await _service.AddEntityAsync(entity);
            if (result.Id > 0)
            {
                //添加帐号信息
                res = await _sysUserService.CreateTenantSysUserAsync(entity, req.loginReq.Username, req.loginReq.Password);
            }
            return GetResponseModel<bool>(res);
        }

        /// <summary>
        /// 检查租户统一社会信用代码是否存在
        /// </summary>
        /// <param name="usci">统一社会信用代码</param>
        /// <returns>是否存在</returns>
        [HttpGet("check-usci")]
        public async Task<ApiResponseModel<bool>> Checkusci([FromQuery] string usci)
        {
            var result = await _sysTenantService.CheckusciExists(usci);
            return GetResponseModel<bool>(result);
        }

        /// <summary>
        /// 获取租户账户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("userinfo")]
        public async Task<ApiResponseModel<GetTenantAdminUserInfoRes>> GetUserInfo([FromQuery] long id)
        {
            var result = await _sysUserService.GetTenantAdminUserInfo(id);
            return GetResponseModel<GetTenantAdminUserInfoRes>(result);
        }

        /// <summary>
        /// 分页获取实体列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns>分页数据</returns>
        [HttpPost("page/list")]
        public async Task<ApiResponseModel<PagedList<TenantsEntity>>> GetPageList([FromBody] GetPageListReq request)
        {
            var result = await _sysTenantService.GetPageList(request);
            return GetResponseModel<PagedList<TenantsEntity>>(result);
        }

        /// <summary>
        /// 修改租户信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public async Task<ApiResponseModel<bool>> UpdateTenantInfo([FromBody] UpdateTenantInfoReq req)
        {
            var userid = _currentUserService.GetUserId();
            var result = await _sysTenantService.UpdateTenantInfo(req, userid);
            return GetResponseModel<bool>(result);
        }

    }
}